import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class News extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return NewsState();
  }
}

class NewsState extends State {
  var data = "";

  _getNews() async {
    var url = "https://wanandroid.com/wxarticle/chapters/json";
    setState(() {
      data = "开始请求$url";
    });
    var response = await Dio().get(url);
    setState(() {
      data = response.data.toString();
    });
    //1s之后删除数据
    const timeout = const Duration(seconds: 3);
    Timer(timeout, () {
      setState(() {
        data = "";
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      TextButton(onPressed: _getNews, child: Text("异步加载")),
      Text(data)
    ]);
  }
}
